{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### TO DO \n",
    "- ajax??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import requests, logging,json\n",
    "from bs4 import BeautifulSoup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#饿了么\n",
    "ele_chuan_hun = 'https://www.ele.me/shop/1363704'       #川魂\n",
    "ele_chuan_xiang = 'https://www.ele.me/shop/161454779'   #川香\n",
    "ele_da_pai = 'https://www.ele.me/shop/157458264'        #大牌冒菜\n",
    "\n",
    "ele_urls=[ele_chuan_hun,ele_chuan_xiang,ele_da_pai]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#jason url\n",
    "e_info='https://www.ele.me/restapi/shopping/restaurant/shop_id?extras[]=activities&extras[]=license&extras[]=identification&extras[]=albums&extras[]=flavors&latitude=31.24667&longitude=117.29009'\n",
    "e_rating='https://www.ele.me/restapi/ugc/v1/restaurants/shop_id/rating_scores?latitude=31.24667&longitude=117.29009'\n",
    "e_rating_catagories='https://www.ele.me/restapi/ugc/v1/restaurant/shop_id/rating_categories'\n",
    "e_base_urls=[e_info,e_rating_catagories,e_rating]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `requests` 获取html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_html(url):\n",
    "    try:\n",
    "        r=requests.get(url,timeout=16)\n",
    "        status=r.status_code\n",
    "        r.raise_for_status()\n",
    "        r.encoding=r.apparent_encoding\n",
    "        return r.text\n",
    "    except:\n",
    "        now_time=time.strftime('%Y%m%d%H%M%S')\n",
    "        logging.info(now_time +'      ' +'get_html 异常, ' '    url：'+ url)   \n",
    "        \n",
    "\n",
    "def get_json(url):\n",
    "    try:\n",
    "        r=requests.get(url,timeout=16)\n",
    "        status=r.status_code\n",
    "        r.raise_for_status()\n",
    "        r.encoding=r.apparent_encoding\n",
    "        return r.json()\n",
    "    except:\n",
    "        now_time=time.strftime('%Y%m%d%H%M%S')\n",
    "        logging.info(now_time +'      ' +'get_html 异常, ' '    url：'+ url) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "eid_ch='1363704'\n",
    "eid_cx='161454779'\n",
    "eid_dp='157458264'\n",
    "eids=[eid_ch,eid_cx,eid_dp]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.ele.me/restapi/shopping/restaurant/1363704?extras[]=activities&extras[]=license&extras[]=identification&extras[]=albums&extras[]=flavors&latitude=31.24667&longitude=117.29009\n",
      "https://www.ele.me/restapi/shopping/restaurant/161454779?extras[]=activities&extras[]=license&extras[]=identification&extras[]=albums&extras[]=flavors&latitude=31.24667&longitude=117.29009\n",
      "https://www.ele.me/restapi/shopping/restaurant/157458264?extras[]=activities&extras[]=license&extras[]=identification&extras[]=albums&extras[]=flavors&latitude=31.24667&longitude=117.29009\n",
      "https://www.ele.me/restapi/ugc/v1/restaurant/1363704/rating_categories\n",
      "https://www.ele.me/restapi/ugc/v1/restaurant/161454779/rating_categories\n",
      "https://www.ele.me/restapi/ugc/v1/restaurant/157458264/rating_categories\n",
      "https://www.ele.me/restapi/ugc/v1/restaurants/1363704/rating_scores?latitude=31.24667&longitude=117.29009\n",
      "https://www.ele.me/restapi/ugc/v1/restaurants/161454779/rating_scores?latitude=31.24667&longitude=117.29009\n",
      "https://www.ele.me/restapi/ugc/v1/restaurants/157458264/rating_scores?latitude=31.24667&longitude=117.29009\n"
     ]
    }
   ],
   "source": [
    "for base_url in e_base_urls:\n",
    "    for id in eids:\n",
    "        url= base_url.replace('shop_id',id)\n",
    "        print(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "url='https://www.ele.me/restapi/shopping/restaurant/161454779?extras[]=activities&extras[]=license&extras[]=identification&extras[]=albums&extras[]=flavors&latitude=31.24667&longitude=117.29009'\n",
    "json_dic=get_json(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'川香冒菜(庐江店)'"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "json_dic['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'attribute': '{\"10\": {\"1\": 2.0, \"0\": 0}}',\n",
       " 'description': '满10减2',\n",
       " 'icon_color': 'f07373',\n",
       " 'icon_name': '减',\n",
       " 'id': 749663010,\n",
       " 'is_exclusive_with_food_activity': True,\n",
       " 'name': '102',\n",
       " 'tips': '满10减2',\n",
       " 'type': 102}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "json_dic['activities'][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'10:30/22:00'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "json_dic['opening_hours'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 页面解析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取目标：\n",
    "- 月售\n",
    "- 热销\n",
    "- 综合评价\n",
    " - 全部评价数量\n",
    " - 满意数量\n",
    " - 不满意数量\n",
    "- 平均送达速度"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
